<template>
  <ul class="todo-main">
    <Item
      v-for="todo in todoList"
      :key="todo.id"
      v-bind="todo"
      :deleteHandle="deleteHandle"
      :checkHandle="checkHandle"
    />
  </ul>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import Item from "./Item";
export default {
  name: "List",

  data() {
    return {
      todoList: [
        { id: uuidv4(), thing: "唱", done: false },
        { id: uuidv4(), thing: "跳", done: true },
        { id: uuidv4(), thing: "rap", done: false },
      ],
    };
  },
  components: {
    Item,
  },
  mounted() {
    //给$bus绑定一个接受header组件传值的事件 newTodo
    this.$bus.$on("newTodo", (newTodo) => {
      this.todoList.unshift(newTodo);
    });
    //调用$bus上的getTodos事件 给footer传递todolist
    this.$bus.$emit("getTodos", this.todoList);
  },
  methods: {
    //单个删除按钮事件
    deleteHandle(id) {
      // console.log(1);
      this.todoList = this.todoList.filter((item) => item.id !== id);
    },
    //单个选择事件
    checkHandle(id) {
      //   console.log(id);
      this.todoList = this.todoList.map((item) => {
        return item.id === id ? { ...item, done: !item.done } : item;
      });
      // console.log(this.todoList);
    },
  },
};
</script>

<style scoped>
/*main*/
.todo-main {
  margin-left: 0px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding: 0px;
}

.todo-empty {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding-left: 5px;
  margin-top: 10px;
}
</style>